# 基类
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
# from selenium.webdriver.support.ui import WebDriverWait
class Base():
    def __init__(self, driver):
        self.driver = driver

    def openurl(self, url):
        self.driver.get(url)

    def find_element(self, loc):
        try:
             WebDriverWait(self.driver, 15, 1).until(lambda driver: driver.find_element(*loc).is_displayed())
             return self.driver.find_element(*loc)
        except Exception as e:
            print(e)
    # 对定位元素进行输入
    def send_keys(self, loc, info):
        self.find_element(loc).send_keys(info)
    # 对定位元素进行键盘事件
    def keys_Enter(self, loc):
        self.find_element(loc).send_keys(Keys.ENTER)
    # frame
    def frame_kj(self, loc):
        self.driver.switch_to.frame(self.find_element(loc))
    # 窗口切换
    def new_window(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
    # 点击事件
    def click(self, loc):
        self.find_element(loc).click()
    # 返回文本
    def get_text(self, loc):
        return self.find_element(loc).text

    def jieping(self, filename):
        # 生成时间戳                     格式          获取当前时间
        timestamp = time.strftime('%Y%m%d%H%M%S', time.localtime())
        # 截屏代码
        self.driver.save_screenshot(timestamp+filename)

    def scr_down(self, a):
        self.driver.execute_script(f'window.scrollTo(0,{a})')
    def clear(self, loc):
        self.find_element(loc).clear()